package com.cdck.safe.dete.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.cdck.safe.dete.common.result.PageResult;
import com.cdck.safe.dete.common.result.Result;
import com.cdck.safe.dete.model.entity.*;
import com.cdck.safe.dete.model.vo.CommunityEchartsVo;
import com.cdck.safe.dete.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author : Sakura
 * @Date : 2024/2/21 9:53
 * @Description :   echarts数据分析
 */
@RestController
@RequestMapping("/api/echarts")
//解决跨域问题
@CrossOrigin
public class EchartsController {

    @Autowired
    private StudentUnionService studentUnionService;

    @Resource
    private NoticeService noticeService;

    @Resource
    private CommunityUserService communityUserService;

    @Resource
    private CommunityService communityService;

    @Resource
    private ActivityService activityService;

    @Resource
    private StudentUnionUserService studentUnionUserService;

    @GetMapping("/sum")
    public Result sum() {
        Map<String, Long> result = new HashMap<>();
        long studentUnionNum = studentUnionService.count();
        result.put("studentUnionNum", studentUnionNum);
        long noticeNum = noticeService.count();
        result.put("noticeNum", noticeNum);
        long communityNum = communityService.count();
        result.put("communityNum", communityNum);
        long activityNum = activityService.count();
        result.put("activityNum", activityNum);
        return Result.success(result);
    }

    /**
     * 饼状图，社团人数比例
     * @param :
     * @return Result
     * @author Sakura
     * @date 2024/3/2 17:55
     */
    @GetMapping("/pie")
    public Result two() {

        List<Community> communityList = communityService.list();
        List<CommunityEchartsVo> communityEchartsVos = communityList.stream().map(community -> {
            CommunityEchartsVo communityEchartsVo = new CommunityEchartsVo();
            communityEchartsVo.setName(community.getCommunityName());

            LambdaQueryWrapper<CommunityUser> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(CommunityUser::getCommunityId, community.getCommunityId());
            communityEchartsVo.setValue(communityUserService.count(wrapper));
            return communityEchartsVo;
        }).collect(Collectors.toList());
        return Result.success(communityEchartsVos);
    }


    /**
     * 柱状图，学生会人数比例
     * @param :
     * @return Result
     * @author Sakura
     * @date 2024/3/2 17:55
     */
    @GetMapping("/pillar")
    public Result three() {
//        两个数组，一个对应x轴，一个对应y轴
        List<StudentUnion> studentUnionList = studentUnionService.list();
        List<String> nameList = studentUnionList.stream().map(item -> item.getUnionName()).collect(Collectors.toList());
        Map<String, List> result = new HashMap<>();
        result.put("nameList", nameList);

        List<Long> numList =new ArrayList<>();
        for (StudentUnion studentUnion : studentUnionList) {
            LambdaQueryWrapper<StudentUnionUser> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(StudentUnionUser::getUnionId, studentUnion.getUnionId());
            numList.add(studentUnionUserService.count(queryWrapper));
        }
        result.put("numList", numList);
        return Result.success(result);
    }

    /**
     * 折现图，近七天活动数量
     * @param :
     * @return Result
     * @author Sakura
     * @date 2024/3/2 17:55
     */
    @GetMapping("/line")
    public Result four() {
//        首先获取近七天的日期
        List<Long> numList = new ArrayList<>();
        Map<String, List> result = new HashMap<>();
        List<String> recentSevenDays = getRecentSevenDays();
        LocalDate currentDate = LocalDate.now();

        for (int i = 6; i >= 0; i--) {
            LocalDate localDate = currentDate.minusDays(i);
//            localDate
            LocalDateTime startOfDay = LocalDateTime.of(localDate, LocalTime.MIN);
            LocalDateTime endOfDay = LocalDateTime.of(localDate, LocalTime.MAX);
            LambdaQueryWrapper<Activity> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.between(Activity::getCreateTime, startOfDay, endOfDay);
            long count = activityService.count(queryWrapper);
            numList.add(count);
        }
        result.put("timeList", recentSevenDays);
        result.put("numList", numList);
        return Result.success(result);
    }

    /**
     * 获取近七天日期
     *
     * @param :
     * @return List<String>
     * @author Sakura
     * @date 2024/1/9 15:56
     */
    public static List<String> getRecentSevenDays() {
        List<String> sevenDays = new ArrayList<>();
        LocalDate currentDate = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM-dd");

        for (int i = 6; i >= 0; i--) {
            sevenDays.add(currentDate.minusDays(i).format(formatter));
        }

        return sevenDays;
    }
}
